<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="模块" prop="moduleId">
        <el-select v-model="queryParams.moduleId" filterable @change="handleChangeModule" placeholder="请选择模块">
          <el-option v-for="item in moduleOptions" :key="item.moduleId" :label="item.moduleName" :value="item.moduleId"
            :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属车间" prop="workshopId">
        <el-select v-model="queryParams.workshopId" filterable @change="handleChangeWorkshop" placeholder="请选择所属车间">
          <el-option v-for="item in workshopOptions" :key="item.workshopId" :label="item.workshopName"
            :value="item.workshopId" :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="大工序" prop="gprocessId">
        <el-select v-model="queryParams.gprocessId" filterable placeholder="请选择大工序">
          <el-option v-for="item in gprocessOptions" :key="item.gprocessId" :label="item.gprocessName"
            :value="item.gprocessId" :disabled="item.status == 1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="proDate">
        <el-date-picker v-model="daterangeEventTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['sl:wagesday:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  border v-loading="loading" :data="wagesdayList" :span-method="arraySpanMethod" @selection-change="handleSelectionChange">

      <el-table-column label="日期" fixed align="center" prop="rworkDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rworkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户" fixed align="center" prop="memberNick" width="70" />
      <el-table-column label="报工单出勤天数" align="center" prop="rworkAttendanceDay" />
      <el-table-column label="报工单结算天数" align="center" prop="rworkSettlementDay" />
      <el-table-column label="报工单总工资" align="center" prop="rworkEstimatePay" />
      <el-table-column label="操作人出勤天数" align="center" prop="attendanceDay" />
      <el-table-column label="出勤工资" align="center" prop="proWages" />
      <el-table-column label="模块" align="center" prop="moduleName" />
      <el-table-column label="车间" align="center" prop="workshopName" />
      <el-table-column label="工序" align="center" prop="gprocessName" />
      <el-table-column label="职级" align="center" prop="levelName" />
      <el-table-column label="计薪方式" align="center" prop="ssmName" />
      <el-table-column label="工作时长" align="center" prop="memberDuration" />
      <el-table-column label="班组" align="center" prop="memberBz" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_rwork_class" :value="scope.row.memberBz" />
        </template>
      </el-table-column>
      <el-table-column label="生产鼓号" align="center" prop="wtermDnumber" />
      <el-table-column label="订单编号" align="center" prop="wtermOnumber" />
      <el-table-column label="品种" align="center" prop="breedName" />
      <el-table-column label="颜色" align="center" prop="colourName" />
      <el-table-column label="工艺要求" align="center" prop="rworkTr" />
      <el-table-column label="单位" align="center" prop="rworkDw" />
      <el-table-column label="计划生产" align="center" prop="rworkPlan" />
      <el-table-column label="实际生产" align="center" prop="rworkActual" />
      <el-table-column label="开始时间" align="center" prop="rworkStart" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rworkStart, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="rworkEnd" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rworkStart, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报工项工作时长" align="center" prop="rworkDuration" />
      <el-table-column label="单价" align="center" prop="processPay" />
      <el-table-column label="报工项工资" align="center" prop="estimatePay" />
      <el-table-column label="报工人" align="center" prop="rfworkNick" />
      <el-table-column label="报工类型" align="center" prop="rfwordType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_rwork_type" :value="scope.row.rfwordType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="扣款金额" align="center" prop="memberDm" /> -->

        <el-table-column fixed="right" label="操作" width="100" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
              v-hasPermi="['sl:rwork:query']">详细</el-button>
          </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


      <!-- 详细对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="日期">{{parseTime(form.rworkDate, '{y}-{m}-{d}') }}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="用户">{{form.memberNick}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="职级">{{form.levelName}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="正式工">
                  <span v-if="form.isFormalworker=='Y'">是</span>
                  <span v-else-if="form.isFormalworker=='N'">否</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="水场小时工">
                  <span v-if="form.isHourlyworker=='Y'">是</span>
                  <span v-else-if="form.isHourlyworker=='N'">否</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="班组">{{form.memberBz}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="工作时长">{{form.memberDuration}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="模块">{{form.moduleName}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="车间">{{form.workshopName}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="大工序">{{form.gprocessName}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="小工序">{{form.processName}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生产鼓号">{{form.wtermDnumber}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="订单编号">{{form.wtermOnumber}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="品种">{{form.breedName}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="颜色">{{form.colourName}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="工艺要求">{{form.rworkTr}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单位">{{form.rworkDw}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="计划生产">{{form.rworkPlan}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="实际生产">{{form.rworkActual}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="开始时间">
                  {{parseTime(form.rworkStart, '{y}-{m}-{d} {h}:{i}:{s}')}}
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="结束时间">
                  {{parseTime(form.rworkEnd, '{y}-{m}-{d} {h}:{i}:{s}')}}
                </el-form-item>
              </el-col>
            </el-row>
<!--            <el-row>
              <el-col :span="8">
                <el-form-item label="扣款金额">{{form.memberDm}}</el-form-item>
              </el-col>
            </el-row> -->
            <el-row>
              <el-col :span="8">
                <el-form-item label="报工项工作时长">{{form.rworkDuration}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="工序单价">{{form.processPay}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="报工项工资">{{form.estimatePay}}</el-form-item>
              </el-col>
            </el-row>
            <el-divider content-position="center">出勤数据（同操作人报工单中报工项汇总）</el-divider>
            <el-row>
              <el-col :span="8">
                <el-form-item label="用户出勤天数">{{form.attendanceDay}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="用户结算天数">{{form.proSettlement}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="报工单总出勤天数">{{form.rworkAttendanceDay}}</el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="报工单总结算天数">{{form.rworkSettlementDay}}</el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="报工单总工资">{{form.rworkEstimatePay}}</el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="出勤工资">{{form.proWages}}</el-form-item>
              </el-col>

            </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">返 回</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
  import {listWagesdayByPiece,listWagesday,getWagesday,delWagesday,addWagesday,updateWagesday} from "@/api/sl/wagesday";
  import { allModule} from "@/api/sl/module";
  import { getWorkshopListByModuleId} from "@/api/sl/workshop";
  import { getGprocessListByWorkshopId} from "@/api/sl/gprocess";
  export default {
    name: "Piece",
    dicts: [ 'sl_rwork_class','sl_rwork_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 日工资表格数据
        wagesdayList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          rfowrkUser: null,
          userName: null,
          nickName: null,
          levelId: null,
          ssmId: null,
          rfworkId: null,
          moduleId: null,
          workshopId: null,
          gprocessId: null,
          workType: null,
          isYpk: null,
          rfworkssmId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 状态时间范围
        daterangeEventTime: [],
        //模块
        moduleOptions: [],
        //车间
        workshopOptions: [],
        //大工序
        gprocessOptions: [],
        spanArr:[], // 需要合并的行数
        pos:0,// 索引
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.getModuleList();
    },
    methods: {
      //行合并
      getSpanArr(data) {
          for (let i = 0; i < data.length; i++) {
              if (i === 0) {
                  this.spanArr.push(1);
                  this.pos = 0
              } else {
                  if (data[i].rfworkId === data[i - 1].rfworkId && data[i].memberUser === data[i - 1].memberUser) {
                      this.spanArr[this.pos] += 1;
                      this.spanArr.push(0);
                  } else {
                      this.spanArr.push(1);
                      this.pos = i;
                  }
              }
          }
      },
      arraySpanMethod({ row, column, rowIndex, columnIndex }){
        if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2||columnIndex === 3||columnIndex === 4||columnIndex === 5||columnIndex === 6) {
              const _row = this.spanArr[rowIndex];
              const _col = _row > 0 ? 1 : 0;
              return {
                  rowspan: _row,
                  colspan: _col
              }
        }
      },
      /** 查询日工资列表 */
      getList() {
        this.loading = true;
        this.spanArr = [];
        this.pos = 0;
        this.queryParams.params = {};
        if (null != this.daterangeEventTime && '' != this.daterangeEventTime) {
          this.queryParams.params["beginEventTime"] = this.daterangeEventTime[0];
          this.queryParams.params["endEventTime"] = this.daterangeEventTime[1];
        }
        this.queryParams.ssmId = "3";
        listWagesdayByPiece(this.queryParams).then(response => {
          this.wagesdayList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.getSpanArr(this.wagesdayList);
        });
      },
      getModuleList() {
        allModule().then(response => {
          this.moduleOptions = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          rworkId: null,
          rworkDate: null,
          memberUser: null,
          memberNick: null,
          levelId: null,
          levelName: null,
          ssmId: null,
          ssmName: null,
          wmId: null,
          wmName: null,
          wmUnit: null,
          memberPay: null,
          isFormalworker: null,
          isHourlyworker: null,
          memberBz: null,
          memberDuration: null,
          moduleId: null,
          moduleName: null,
          workshopId: null,
          workshopName: null,
          gprocessId: null,
          gprocessName: null,
          processId: null,
          processName: null,
          wtermDnumber: null,
          wtermOnumber: null,
          breedName: null,
          colourName: null,
          rworkTr: null,
          rworkDw: null,
          rworkPlan: null,
          rworkActual: null,
          rworkRepactual: null,
          rworkRepetitions: null,
          rworkStart: null,
          rworkEnd: null,
          rworkDuration: null,
          processPay: null,
          estimatePay: null,
          rfworkId: null,
          rfworkUser: null,
          rfworkNick: null,
          leaderUser: null,
          leaderNick: null,
          rfwordType: null,
          attendanceDay: null,
          proSettlement: null,
          rworkAttendanceDay: null,
          rworkSettlementDay: null,
          proWages: null,
          memberDm: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.wagesDayId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('sl/wagesday/exportp', {
          ...this.queryParams
        }, `wagesday_${new Date().getTime()}.xlsx`)
      },
      //模块变更查询车间
      handleChangeModule(value) {
        //清空车间
        this.workshopOptions = [];
        this.queryParams.workshopId = null;
        //清空大工序
        this.gprocessOptions = [];
        this.queryParams.gprocessId = null;
        getWorkshopListByModuleId(value).then(response => {
          this.workshopOptions = response.data;
        });
      },
      //车间变更查询大工序
      handleChangeWorkshop(value) {
        //清空大工序
        this.gprocessOptions = [];
        this.queryParams.gprocessId = null;
        getGprocessListByWorkshopId(value).then(response => {
          this.gprocessOptions = response.data;
        });
      },
      /** 详细按钮操作 */
      handleDetail(row) {
        this.reset();
        const rworkId = row.rworkId;
        const rfworkId = row.rfworkId;
        const itemDetail = this.wagesdayList.filter(function(item) {
          if(item.rworkId===rworkId && item.rfworkId===rfworkId){
            return item
          }
          return null;
        });
        if(null != itemDetail.length){
          this.form = itemDetail[0]
        }
        this.open = true;
        this.title = "详细";
      },
    }
  };
</script>
<style>
.el-table .el-table__body-wrapper::-webkit-scrollbar {
    height: 10px;
}
</style>

